package com.ruoyi.wisdom.task;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.wisdom.domain.swap.SwapPickGroup;
import com.ruoyi.wisdom.event.PickGroupEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class PickGroupTask extends IotTask{

    @Autowired
    private PickGroupEvent pickGroupEvent;

    @Scheduled(fixedRate = 10000) // 每10秒执行一次
    public void pullTask() {
        try {
            Long length = redisSecondTemplate.opsForList().size(iotConstants.getPullPickGroup());
            if (length > 0) {
                while (length > 0) {
                    Object obj = redisSecondTemplate.opsForList().leftPop(iotConstants.getPullPickGroup());
                    if (obj != null) {
                        String objJson = obj.toString();
                        SwapPickGroup swapPickGroup = JSON.parseObject(objJson, SwapPickGroup.class);
                        pickGroupEvent.handle(swapPickGroup);
                    }
                    length--;
                }
            }
        } catch (Exception e) {
            printErrorLog("拉取场景数据-- redis 队列-异常：" + e.getMessage());
        }
    }
}
